'''
Created on 14 Feb 2010

@author: as371
'''
import os,string
import shutil

def log(*s):
    print string.join(s, " ")
    

def createFolder(mypath):
    if not os.path.isdir(mypath):
        os.makedirs(mypath)



def writeToFile(file_name, content):
    with open(file_name,"w" ) as f:
        for row in content:
            f.write("%s\n" % str(row))
            
            
            
def createLabelsMLF(entries):
    fileContent = []
    fileContent.append("#!MLF!#")
    for container, path,label in entries:
        fileContent.append('"%s/%s/%s.wav"' % (container,path,label))
        fileContent.append(label)
        fileContent.append('.')
    return fileContent
        
def createFileList(entries, folder):
    wavFiles = []
    mfcFiles = []
    labelFiles = []
    for container, path,label in entries:
        localFolder = "%s/%s" % (folder,path)
        if  os.path.exists(localFolder): 
            #os.rmdir(localFolder)
            shutil.rmtree(localFolder)
        os.makedirs(localFolder)
        wavFiles.append('%s/%s/%s.wav' % (container,path,label))
        #creating label files for each wav 
        labelFiles.append(('%s/%s/%s.lab' % (container,path,label), label))
        mfcFiles.append("%s/%s/%s.mfc" % (folder,path,label))
    return (wavFiles,mfcFiles,labelFiles)
        
def getFileList(srcFolder,container, folder):
    labels = []
    path = "%s/%s/%s" % (srcFolder,container, folder)
    for root, dirs, files in os.walk(path):
        if root == path:
            #log("root", root)
            for name in files:
                if string.find(name,'.wav') != -1: 
                    label = string.replace(name, '.wav', '')
                    #log('file',name,'label' ,label)
                    labels.append((container, folder, label))
        
    return labels
    
def prepareFileList(wavFiles,mfcFiles):
    fileListContent = []
    for file in zip(wavFiles,mfcFiles):
        wavFile, mfcFile = file
        #wav file entry
        fileListContent.append(wavFile)
        #mfc file entry
        fileListContent.append(mfcFile)
        
    return fileListContent

def createLabelFiles(labelFiles):
    for label in labelFiles:
        fileName, content = label
        writeToFile(fileName, [content])